Program Blocks and Block Scope
As mentioned in An Example Script, a script can be referred to as a program block, which is the basic unit of VectorScript source code. Program blocks consist of a block declaration statement, sections such as the CONST, TYPE or VAR blocks for declaring or defining data within the block, and the body of the block, which contains the VectorScript source code to be executed. User-defined functions extend this concept, and are in fact smaller program blocks nested within the main program block that is your script.
Each subroutine that will be used in a script is a self-contained program block, with its own data declarations and body. Subroutine blocks can also have nested subroutine blocks of their own, with other data declarations and script code. Such nesting of subroutine blocks brings up an important concept, block scope, that should be considered whenever writing subroutines for scripts.
Block scope describes the area of a script where a given identifier is considered valid and has a defined value associated with it. Whenever a variable, constant, or structure is declared in a program block, the item is said to be local to that program block. This means that the item will only be valid and have a defined value in the block where it was declared, as well as in any areas which are enclosed by that block. For example:
 
In the example, the scope of an identifier is determined by its location:
 
An identifier is considered undefined outside the program block where it was declared and may not be accessed or referred to in script code outside of the block. If the block in which the identifier is declared is a subroutine, this means that the identifier will be undefined in any block enclosing the subroutine. Any attempt to refer to or evaluate the item from source code in the blocks enclosing the subroutine will cause an error and will cause the script to fail.
The following example also illustrates the concept of block scope:
 
{ begin CalcCost code }
{ end CalcCost code }
{ begin main script }
{ end main script }
In the example there are three program blocks, or areas of scope. The largest block is the main script, WoodPrice; contained within it is the subroutine block CalcCost, and within CalcCost is the subroutine function and program block AddTax.
Any variable or constant identifiers defined in the WoodPrice block can be referred to in the WoodPrice script code, and can also be referenced from within any of the subroutines declared within the block. These items are said to have global scope because they are defined at the top level of the script, and can be accessed from any subroutine within the script.
Identifiers defined in the CalcCost subroutine (including those in the declaration statement) can be referred to in the CalcCost subroutine, or within the AddTax function. They are undefined, however, in the WoodPrice block, which lies outside the CalcCost scope. This means that items such as baseCost or the subroutine AddTax cannot be referenced directly from the main body of the WoodPrice script.
The identifiers defined in the AddTax subroutine have the smallest scope of any of the blocks in the script; they are available only to code contained within that subroutine. They are undefined for and cannot be referenced from the CalcCost and WoodPrice program blocks. In the example, the kTax constant can be referenced directly in the AddTax function because kTax is defined in the main script and has global scope. The result of AddTax, however, cannot be accessed directly from the main script, since it is declared within the CalcCost subroutine and is only valid within that subroutine.

User Defined Functions : Program Blocks and Block Scope

Nemetschek NA
Phone: 410.290.5114
Fax: 410.290.8050